<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use bright_tech\laravel\aliyun_oss\AliyunOssService;
/**
 * Class Teacher
 *
 * @package App\Models
 * @property int $id
 * @property int $user_id 用户id
 * @property int $id_card 身份证号码
 * @property string $name 姓名
 * @property string $mobile 手机号码
 * @property string $job 职称
 * @property string $created_at
 * @property string $updated_at
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Teacher whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Teacher whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Teacher whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Teacher whereMobile($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Teacher whereJob($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Teacher whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Teacher whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Teacher extends Model
{
    use SoftDeletes;

    protected $table = 'xyj_teachers';

    protected $primaryKey = 'user_id';


    protected $fillable = [
        'name',
        'id_card',
        'mobile',
        'qq',
        'email',
        'school_id',
        'dept_id',
        'speciality_id',
        'province_id',
        'city_id',
        'district_id',
        'job',
        'positional_title',
        'leading',
        'chuangke',
        'adviser',
        'judge',
        'ems',
        'remarks',
        'personal_introduction',
        'portrait',
        'finish_school_id',
        'finish_dept_id',
        'finish_speciality_id',
        'finish_province_id',
        'finish_city_id',
        'certificates_type',        //证件类型
        'alipay',
        'school_year'

    ];


    /**
     * 该老师参与的作品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function works()
    {
        return $this->belongsToMany('\App\Models\Works', 'works_teacher', 'teacher_id', 'works_id');
    }

    public function user()
    {
        return $this->belongsTo('\App\Models\User', 'user_id');
    }


    public function getHonorIds(){
        $honors = DB::table('honor_rate_teacher')->where(['type' =>'teacher' , 'user_id' => $this->user_id])->lists('honor_rate_teacher.honor_id');
        return $honors;
    }

    public static function getGgrTeacherList()
    {
        $resultList = [];
        $teacherList = self::all();
        foreach ($teacherList as $teacher){
            $resultList[$teacher->id] = $teacher->name.'-'.$teacher->mobile.'-'.$teacher->school_cn;
        }
        return $resultList;
    }


    /**
     * 教师详情头像
     * @return string
     */
    public function getPortraitImageUrl()
    {
        $app = app();
        $aliyun = $app->make(AliyunOssService::class);
        $host = $aliyun->getOssUtilities()->getImageViewEndpoint();
        return $host . '/' . $this->portrait . '@360h_270w_2e';
    }



    /**
     * 教师列表头像
     * @return string
     */
    public function getPortraitThumbUrl()
    {
        $app = app();
        $aliyun = $app->make(AliyunOssService::class);
        $host = $aliyun->getOssUtilities()->getImageViewEndpoint();
        return $host . '/' . $this->portrait . '@200h_150w_2e';
    }




}